// mainPackage/pages/evaluate/evaluate.js
const app = getApp()

import util from "../../../utils/words";
Page({

  /**
   * 页面的初始数据
   */
  data: {
    value: 0,
    numText: '零',
    text: '',
    userPhoto: '',
    fileList: [],
    productId: 0,
    orderNum: '', //当前评价的订单号
  },


  // 改变商品的评价状态
  chgangeProStatus() {
    var url = app.globalData.url + "ScOrder/rateProduct",
      that = this;
    app.wxRequest(url, {
        productId: that.data.productId,
        orderNum: that.data.orderNum,
      }, res => {
        console.log(res);
        if (res.flag) {} else {}
      },
      err => {
        console.log(err);
      })
  },


  // 提交
  submit() {

    var fileList = this.data.fileList,
      that = this;
    if (fileList.length > 6) {
      wx.showToast({
        title: '最多上传6张图片',
        icon: 'none'
      })
      return
    }


    console.log(this.data.value);
    console.log(this.data.text);
    console.log(fileList);

    var value = this.data.value,
      text = this.data.text,
      start = 0;


    if (util.filter_method(text).split("/")[1] == 1) {
      wx.showToast({
        title: '当前评论涉及到敏感词汇,请重新编辑',
        icon: 'none'
      })
      return
    }

    console.log(value);
    if (value < 2) {
      start = 2
    } else if (value < 3) {
      start = 1
    } else {
      start = 0
    }
    console.log(start);
    var obj = {
      commentType: 0,
      commentContent: text,
      commentUserId: app.globalData.userId,
      companyId: app.globalData.companyId,
      productId: this.data.productId,
      commentStatus: 1,
      imageList: []
    }

    if (fileList.length != 0) {
      that.addPic(obj, fileList);
    } else {
      that.addTalk(obj)
    }






  },


  addPic(obj, list) {
    var that = this;
    var j = 0;
    wx.showLoading();
    for (var i in list) {
      console.log(i + "哈哈");
      console.log();
      wx.uploadFile({
        url: app.globalData.url + "ScComment/addCommentImg",
        filePath: list[i].tempFilePath,
        name: 'file',
        success(res) {
          console.log(res);
          var result = JSON.parse(res.data);
          console.log(j);
          console.log(result);
          obj.imageList.push({
            commentImage: result.data
          })
          console.log(obj);
          j++;
          if (j == (list.length)) {
            console.log('jinlai');
            that.addTalk(obj);
          }
        }
      })
    }
  },
  addTalk(obj) {
    console.log(obj);

    var url = app.globalData.url + "ScComment/addComment",
      that = this;

    app.wxRequestJson(url, obj, res => {
        console.log(res);
        if (res.flag) {
          that.chgangeProStatus()
          wx.showModal({
            title: '提示',
            content: '评价成功',
            showCancel: false,
            complete: (res) => {
              if (res.confirm) {
                wx.redirectTo({
                  url: '/userPackage/pages/order/order?index=' + 3,
                })
              }
            }
          })
        } else {
          wx.showToast({
            title: '发布失败,请重试',
            icon: 'none'
          })
        }

      },
      err => {
        console.log(err);
      })
  },

  onChange(e) {
    console.log(e);
    this.setData({
      value: e.detail,
      numText: this.numberToChinese(e.detail)
    })
  },

  delHandle(e) {
    var index = e.currentTarget.dataset.index,
      fileList = this.data.fileList;
    fileList.splice(index, 1)
    console.log(fileList);
    this.setData({
      fileList
    })
  },

  // 上传头像
  upHandle() {
    var that = this,
      fileList = this.data.fileList;
    console.log(this.data.fileList);
    wx.chooseMedia({
      count: 6,
      mediaType: ['image'],
      sourceType: ['album', 'camera'],
      camera: 'back',
      success(res) {
        console.log(res.tempFiles)
        fileList.push(...res.tempFiles)
        that.setData({
          fileList,
        })
        console.log(that.data.fileList);
      }
    })
  },



  // 输入框编辑
  inputHandle(e) {
    var str = e.currentTarget.dataset.flag
    var val = e.detail.value
    this.setData({
      [str]: val
    })
    console.log(this.data);
  },

  numberToChinese(num) {
    const chineseNums = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九'];
    const chineseUnits = ['', '十', '百', '千'];

    if (num === 0) {
      return chineseNums[0];
    }

    let chineseStr = '';
    let unitIndex = 0;

    while (num > 0) {
      const digit = num % 10;
      if (digit !== 0) {
        // 处理非零数字
        chineseStr = chineseNums[digit] + chineseUnits[unitIndex] + chineseStr;
      } else if (chineseStr.charAt(0) !== chineseNums[0]) {
        // 处理连续的零，只保留一个零
        chineseStr = chineseNums[0] + chineseStr;
      }

      num = Math.floor(num / 10);
      unitIndex++;
    }

    return chineseStr;
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options);
    this.setData({
      productId: Number(options.id),
      orderNum: options.orderNum,
    })
    console.log(this.data.productId);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})